<?php
$this->load->view('admin/layout/head', array('title'=>"产品管理",'nav'=>'product','css'=>['/assets/select2/css/select2.min.css']), FALSE);
?>

<div id="container">
	<form class="form-inline" id="search-form" method="GET" action="/admin/product">
	  <div class="form-group">
	  	<select name="category_id" id="category_id_slt" class="form-control">
	  		<option value=""><?= $this->lang->line('product_category'); ?></option>
	  		<?php foreach($categorys as $cate): ?>
	  		<option value="<?php echo $cate['id']?>" <?= $this->input->get('category_id') != $cate['id'] ?: 'selected' ?>><?php echo $cate['title'] ?></option>
	  		<?php endforeach; ?>
	  	</select>
	  	<select name="category_id2" id="category_id2_slt" class="form-control">
	  		<option value=""><?= $this->lang->line('sub_categories') ?></option>
	  		<?php foreach ($sub_categories as $value): ?>
	  			<option value="<?= $value['id'] ?>" <?= $this->input->get('category_id2') != $value['id'] ?: 'selected' ?>> <?= $value['title'] ?></option>
	  		<?php endforeach ?>
	  	</select>
	  </div>
	  <div class="form-group">
	  	<select name="brand_id" class="brand-slt" class="form-control">
	  		<option value=""><?= $this->lang->line('choose_brand') ?></option>
	  		<?php foreach($brands as $b): ?>
	  		<option value="<?php echo $b['id']?>" <?= $this->input->get('brand_id') != $b['id'] ?: 'selected' ?>><?php echo $b['title'] ?></option>
	  		<?php endforeach; ?>
	  	</select>
	  </div>
	  <div class="form-group">
	  	<select name="status" class="form-control">
	  		<option value=""><?= $this->lang->line('status') ?></option>
	  		<option value="0" <?= $this->input->get('status') !== '0' ?: 'selected' ?>><?=  $this->lang->line('put off shelves') ?></option>
	  		<option value="1" <?= $this->input->get('status') !== '1' ?: 'selected' ?>><?= $this->lang->line('put on shelves'); ?></option>
	  		<option value="2" <?= $this->input->get('status') !== '2' ?: 'selected' ?>>库存不足</option>
	  		<option value="3" <?= $this->input->get('status') !== '3' ?: 'selected' ?>>库存过期</option>
	  	</select>
	  </div>
	  <div class="form-group">
	    <input type="text" name="keyword" placeholder="<?= $this->lang->line('enter_product_name_search') ?>" class="form-control" style="width:300px" placeholder="">
	  </div>
	  <a href="javascript:;" class="btn btn-info search-btn"><?= $this->lang->line('search') ?></a>
	  <a href="/admin/product/create" class="btn btn-default add-btn"><span class="glyphicon glyphicon-plus"></span> <?= $this->lang->line('create_product') ?></a>
	</form>

	<div style="margin-top: 20px;" class="toolbar">
		<select  name="batch_shangjia" style="width:100px; display:inline-block;" class="form-control">
			<option value="1"><?= $this->lang->line('put on shelves') ?></option>
			<option value="0"><?= $this->lang->line('put off shelves') ?></option>
		</select>
		<button type="button" class="multi_shangjia btn btn-default">批量上下架</button>

		<select name="multi_category_id" id="category_id1_slt" style="margin-left: 30px; width:100px; display:inline-block;" class="form-control">
	  		<option value=""><?= $this->lang->line('product_category'); ?></option>
	  		<?php foreach($categorys as $cate): ?>
	  		<option value="<?php echo $cate['id']?>" <?= $this->input->get('category_id') != $cate['id'] ?: 'selected' ?>><?php echo $cate['title'] ?></option>
	  		<?php endforeach; ?>
	  	</select>
	  	<select name="multi_category_id2" id="category_id3_slt" style="width:100px; display:inline-block;" class="form-control">
	  		<option value=""><?= $this->lang->line('sub_categories') ?></option>
	  		<?php foreach ($sub_categories as $value): ?>
	  			<option value="<?= $value['id'] ?>" <?= $this->input->get('category_id2') != $value['id'] ?: 'selected' ?>> <?= $value['title'] ?></option>
	  		<?php endforeach ?>
	  	</select>
		<button type="button" class="multi_fenlei btn btn-default">批量分类</button>
	</div>
	<div class="table-container mt-10">
		<table class="table table-bordered table-striped table-hover scroll-table">
			<thead >
				<tr>
					<th width="80">id</th>
					<th width="240"><?= $this->lang->line('title') ?></th>
					<th width="240" class="text-center"><?= $this->lang->line('category') ?></th>
					<th width="100" class="text-center"><?= $this->lang->line('brand') ?></th>
					<th width="60" class="text-center"><?= $this->lang->line('price') ?></th>
					<th width="60" class="text-center"><?= $this->lang->line('stock') ?></th>
					<th width="60" class="text-center"><?= $this->lang->line('sales') ?></th>
					<th width="60" class="text-center"><?= $this->lang->line('status') ?></th>
					<th width="60" class="text-center"><?= $this->lang->line('sort') ?></th>
					<th width="80" class="text-center"><?= $this->lang->line('actions') ?></th>
				</tr>
			</thead>
			<tbody class="text-center">
				<?php foreach($products as $q): ?>
				<tr id='tr-<?php echo $q['id']?>'>
					<td>
						<input type="checkbox" name="ids[]" value="<?= $q['id']?>">
					</td>
					<td class="text-left"><?php echo $q['title'] ?></td>
					<td><?php if($q['category_id'] && isset($product_cates[$q['category_id']])) echo $product_cates[$q['category_id']]['title'] ?> - <?php if($q['category_id2'] && isset($product_cates[$q['category_id2']])) echo $product_cates[$q['category_id2']]['title'] ?></td>
					<td ><?php if($q['brand_id']) echo $brands[$q['brand_id']]['title'] ?></td>
					<td>￥<?php echo $q['price'] ?></td>
					<td>
						<?php echo $q['stock']*1 ?>
						<?php if(in_array($q['id'], $dangers)): ?>
						<span style="color: #f30">不足</span>
						<?php endif; ?>
						<?php if(in_array($q['id'], $expires)): ?>
						<a href="/admin/purchaseRecord/index?expire=1&product_id=<?php echo $q['id'] ?>" style="color:#f30">过期</span>
						<?php endif; ?>
					</td>
					<td><?php echo $q['solded']*1 ?></td>
					<td>
						<a href="javascript:;" product_id="<?php echo $q['id']?>"><?php echo $q['status']? $this->lang->line('put on shelves') :'<span style="color:#f00">'.$this->lang->line('put off shelves').'</span>' ?></a>

					</td>
					<td><?php echo $q['sort'] ?></td>
					<td>
						<a href="/admin/product/edit/<?php echo $q['id'] ?>" class="modify-btn"><?= $this->lang->line('edit') ?></a> |
						<a href="javascript:;" class="del-btn" help_id="<?php echo $q['id'] ?>"><?= $this->lang->line('delete') ?></a>
					</td>
				</tr>
				<?php endforeach; ?>
			</tbody>
			<?php
			if($products->last_page>1){
				//$products->url = current_url();
			?>
			<tfoot>
				<td colspan="9" style="text-align:right">
					<?php echo $products->render() ?>
				</td>
			</tfoot>
			<?php
			}
			?>
		</table>
	</div>
</div>


<?php
$this->load->view('admin/layout/footer',['js'=>['/assets/select2/js/select2.full.min.js']]);
?>

<script type="text/javascript">
	function checkids() {
		var ids = [];
		var ids_doms = $('[name="ids[]"]');

		for (k in ids_doms) {
			if (ids_doms[k].checked) {
				var id = ids_doms.eq(k).val();
				ids.push(id);
			}
		}

		if (ids.length < 1) {
			alert('请选择商品');
			return false;
		}

		return ids;
	}
	$(function(){
		$('.brand-slt').select2();
		$('.del-btn').on('click',function(){
			var help_id = $(this).attr('help_id');
			confirm('<?= $this->lang->line('Are you sure delete it') ?>',function(e){
				if(e.index == 1){
					Toast.loading();
					$.post('/admin/product/delete', {id: help_id}, function(data, textStatus, xhr) {
						Toast.destory();
						Toast.success('<?= $this->lang->line('success') ?>',function(){
							window.location.reload();
						})
					});
				}
			})
		});

		$('.search-btn').on('click',function(){
			$('#search-form').submit();
		})

		var hash = window.location.hash;
		if(hash){
			$(hash).css('background-color','#ccc');
		}

		$('#category_id_slt').on('change',function(){
			var parent_id = $('#category_id_slt').val();
			if($('#category_id_slt').val()){
				$.getJSON('/admin/category/get_categorys/'+parent_id,function(json){
					$('#category_id2_slt').html('<option value=""><?= $this->lang->line('sub_categories') ?></option>');
					for(var i=0;i<json['data'].length;i++){
						$('#category_id2_slt').append('<option value="'+json['data'][i].id+'">'+json['data'][i].title+'</option>')
					}
				})
			}else{
				$('#category_id2_slt').html('<option value=""><?= $this->lang->line('sub_categories') ?></option>');
			}
		})

		$('#category_id1_slt').on('change',function(){
			var parent_id = $('#category_id1_slt').val();
			if($('#category_id1_slt').val()){
				$.getJSON('/admin/category/get_categorys/'+parent_id,function(json){
					$('#category_id3_slt').html('<option value=""><?= $this->lang->line('sub_categories') ?></option>');
					for(var i=0;i<json['data'].length;i++){
						$('#category_id3_slt').append('<option value="'+json['data'][i].id+'">'+json['data'][i].title+'</option>')
					}
				})
			}else{
				$('#category_id3_slt').html('<option value=""><?= $this->lang->line('sub_categories') ?></option>');
			}
		})

		$('[name=keyword]').focus();

		$('.multi_shangjia').on('click', function () {
			var ids = checkids();
			if (!ids) {
				return;
			}


			confirm('您确定要上/下架选中的商品吗？', function (res) {
				if (res) {
					var value = $('[name=batch_shangjia]').val();

					$.post('/admin/product/multi_shangjia', {
						ids: ids,
						value: value
					}, function () {
						alert('编辑成功');
						window.location.reload();
					});
				}
			});
		});

		$('.multi_fenlei').on('click', function () {
			var ids = checkids();
			if (!ids) {
				return;
			}
			confirm('您确定要为选中的商品设置分类吗？', function (res) {
				if (res) {
					var category_id1 = $('[name=multi_category_id]').val();
					var category_id2 = $('[name=multi_category_id2]').val();

					$.post('/admin/product/multi_fenlei', {
						ids: ids,
						category_id1: category_id1,
						category_id2: category_id2
					}, function () {
						alert('编辑成功');
						window.location.reload();
					});
				}
			});
		});
	})
</script>
